<template>
  <div>
    <Row style="padding-bottom: 50px ;;background-repeat: no-repeat;
  background-size: 100% 100%; margin-top: -60px;" :style="{'background-image':'url('+ img_reg+')'}">
      <i-col :xs="2" :sm="4" :md="6" :lg="8" offset="14"
             style="background-color: white;padding-right: 110px;margin-top: 120px;">
        <Row>
          <i-col style="text-align: center;">
            <h4 style="margin: 20px 0px 20px 120px;">注册账号</h4>
          </i-col>
        </Row>
        <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="120">
          <FormItem label="用户名" prop="userName">
            <Input v-model="formValidate.userName" placeholder="请输入用户名"></Input>
          </FormItem>
          <FormItem label="手机号" prop="mobile">
            <Input v-model="formValidate.mobile" placeholder="请输入手机号"></Input>
          </FormItem>
          <FormItem label="验证码" prop="verificationCode">
            <Row>
              <i-col :lg="14">
                <Input v-model="formValidate.verificationCode" placeholder="请输入验证码"></Input>
              </i-col>
              <i-col :lg="8" offset="2">
                <Button type="primary" v-if="is_send == 0" @click="sendVerificationCode('formValidate')">
                  {{formValidate.send}}
                </Button>
                <Button type="primary" disabled v-if="is_send == 1">{{second}}</Button>
              </i-col>
            </Row>
          </FormItem>
          <FormItem label="密码" prop="pwd">
            <Input v-model="formValidate.pwd" type="password" placeholder="请输入密码" autocomplete="off"></Input>
          </FormItem>
          <FormItem label="确认密码" prop="confirmPwd">
            <Input v-model="formValidate.confirmPwd" type="password" placeholder="请输入确认密码" autocomplete="off"></Input>
          </FormItem>
          <FormItem label="企业名称" prop="companyName">
            <Input v-model="formValidate.companyName" placeholder="请输入营业执照全称"></Input>
          </FormItem>
          <FormItem label="上传营业执照" prop="voucher">
            <!--<Upload
                      :before-upload="handleUpload"
                      action="//jsonplaceholder.typicode.com/posts/">
                      <Button type="ghost" icon="ios-cloud-upload-outline">选择文件</Button>
                      <div v-if="file !== null">上传文件: {{ file.name }}</div>
                  </Upload>-->
            <div>
              <div class="demo-upload-list" v-for="item in uploadList">
                <img :src="item.url">
                <div class="demo-upload-list-cover">
                  <Icon type="ios-trash-outline" @click.native="handleRemove(item)"></Icon>
                </div>
              </div>
              <Upload ref="upload"
                      :show-upload-list="false"
                      :format="['jpg','jpeg','png','bmp']"
                      :max-size="1024"
                      :before-upload="handleBeforeUpload"
                      :on-format-error="handleFormatError"
                      :on-exceeded-size="handleMaxSize"
                      accept='image/*'
                      multiple
                      type="drag"
                      action="//jsonplaceholder.typicode.com/posts/"
                      style="display: inline-block;width:58px;">
                <div style="width: 58px;height:58px;line-height: 58px;text-align: center;">
                  <Icon type="ios-cloud-upload-outline" size="30"></Icon>
                </div>
              </Upload>
              <div style="font-family: PingFangSC-Regular;font-size: 12px;color: #AAAAAA;letter-spacing: 0;">只能上传
                jpg/png 文件，且不超过 1 MB
              </div>
            </div>
          </FormItem>
          <FormItem label="真实姓名" prop="realName">
            <Input v-model="formValidate.realName" placeholder="请输入真实姓名"></Input>
          </FormItem>
          <FormItem label="职务" prop="job">
            <Input v-model="formValidate.job" placeholder="请输入您目前的职务名称"></Input>
          </FormItem>
          <FormItem label="联系邮箱" prop="email">
            <Input v-model="formValidate.email" placeholder="请输入联系邮箱"></Input>
          </FormItem>
          <!-- <FormItem label="" prop="agreement">
                    <CheckboxGroup v-model="formValidate.agreement">
                        <Checkbox label="是否遵守用户协议"></Checkbox>
                    </CheckboxGroup>
                </FormItem> -->
          <FormItem>
            <Row>
              <i-col style="text-align: center;">
                <Button type="primary" @click="handleSubmit('formValidate')">提交</Button>
                <Button type="ghost" @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
              </i-col>
            </Row>
          </FormItem>
        </Form>
      </i-col>
    </Row>
  </div>
</template>
<script>
  import { sendSms } from '@api/common'
  import { mapGetters } from 'vuex'
  import axios from 'axios'
  import imgRegs from '@static/img/img_reg.jpg'

  export default {
    name: 'index',
    data () {
      return {
        img_reg: imgRegs,
        second: 60,
        is_send: 0,
        file: null,
        formValidate: {
          userName: '',
          mobile: '',
          verificationCode: '',
          pwd: '',
          companyName: '',
          realName: '',
          email: '',
          send: '获取验证码',
          confirmPwd: ''
        },
        uploadList: [],
        ruleValidate: {
          companyName: [
            { required: true, message: '企业名称不能为空！', trigger: 'blur', whitespace: true, type: 'string' }
          ],
          userName: [
            { required: true, message: '用户名不能为空！', trigger: 'blur', whitespace: true, type: 'string' },
            { pattern: /^[a-zA-Z0-9_\-\u4e00-\u9fa5]{3,11}$/, message: '请输入4-12 位字符(汉字、字母、数字及“-”、“_”组合)！', trigger: 'blur' }
          ],
          mobile: [
            { required: true, message: '手机号不能为空！', trigger: 'blur', whitespace: true, type: 'string' },
            { pattern: /^1[0-9]{10}$/, message: '手机号格式不正确！', trigger: 'blur' }
          ],
          verificationCode: [
            { required: true, message: '验证码不能为空！', trigger: 'blur', whitespace: true, type: 'string' }
          ],
          pwd: [
            { required: true, message: '密码不能为空！', trigger: 'blur' },
            { type: 'string', message: '密码不能小于6位！', min: 6, trigger: 'blur' }
          ],
          confirmPwd: [
            { required: true, message: '确认密码不能为空！', trigger: 'blur', whitespace: true, type: 'string' }
          ],
          realName: [
            { required: true, message: '真实姓名不能为空！', trigger: 'blur', whitespace: true, type: 'string' }
          ],
          email: [
            { required: true, message: '联系邮箱不能为空！', trigger: 'blur', whitespace: true, type: 'string' },
            { type: 'email', message: '邮箱格式不正确！', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      handleBeforeUpload (file) {
        let reader = new FileReader()
        reader.readAsDataURL(file)
        const _this = this
        reader.onloadend = function (e) {
          if (file.size <= 1024 * 1024) {
            if (file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/jpg' || file.type === 'image/bmp') {
              file.url = reader.result
              _this.uploadList.push(file)
            }
          }
        }
        return false
      },
      handleRemove (file) {
        this.uploadList.splice(this.uploadList.indexOf(file), 1)
      },
      handleFormatError (file) {
        this.$Notice.warning({
          title: '文件格式不正确',
          desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
        })
      },
      handleMaxSize (file) {
        this.$Notice.warning({
          title: '超出文件大小限制',
          desc: '文件 ' + file.name + ' 太大，不能超过 1M。'
        })
      },
      sendVerificationCode (name) {
        this.$refs[name].validateField('mobile', (valid) => {
          if (valid.trim() === '') {
            let mobilePhone = this.formValidate.mobile
            let type = 'register'
            let param = { mobilePhone, type }
            sendSms(param).then(res => {
              if (res.data.isSuccess === 1) {
                this.$Message.success('发送成功!')
                this.is_send = 1
                this.jian()
              } else {
                this.$Message.error(res.data.res)
              }
            })
          }
        })
      },
      handleSubmit (name) {
        this.$refs[name].validate((valid) => {
          if (valid) {
            let pwd = this.formValidate.pwd
            let confirmPwd = this.formValidate.confirmPwd
            if (pwd !== confirmPwd) {
              this.$Message.error('确认密码与密码不一致!')
              return false
            }
            let formData = new FormData()
            formData.append('username', this.formValidate.userName)
            formData.append('mobilePhone', this.formValidate.mobile)
            formData.append('verifyCode', this.formValidate.verificationCode)
            formData.append('password', this.formValidate.pwd)
            formData.append('companyName', this.formValidate.companyName)
            formData.append('realName', this.formValidate.realName)
            formData.append('email', this.formValidate.email)
            if (this.formValidate.job != null) {
              formData.append('job', this.formValidate.job)
            }
            if (this.uploadList.length >= 6) {
              this.$Notice.warning({
                title: '图片不得超过5张'
              })
              return false
            }
            if (this.uploadList && this.uploadList.length > 0 && this.uploadList.length < 6) {
              this.uploadList.forEach((file) => {
                formData.append('imgs', file)
              })
            }
            axios({
              url: process.env.BASE_API + '/register',
              method: 'post',
              data: formData,
              headers: { 'Content-Type': 'multipart/form-data' }
            })
              .then((res) => {
                console.log(res)
                if (res.data.isSuccess === 1) {
                  this.$Message.success('注册成功!')
                  let param = {
                    username: this.formValidate.userName,
                    password: this.formValidate.pwd
                  }
                  return this.$store.dispatch('doLogin', param)
                } else {
                  this.$Message.error(res.data.res)
                }
              })
              .then((res) => {
                let resData = res.data
                if (resData.data && resData.data.user && +resData.data.user.status === 2) {
                  this.$router.push({
                    path: '/wait'
                  })
                } else {
                  this.$router.push({
                    path: '/'
                  })
                }
              })
          } else {
            this.$Message.error('注册失败!')
          }
        })
      },
      handleReset (name) {
        this.$refs[name].resetFields()
      },
      jian () {
        var that = this
        setTimeout(function () {
          that.second = that.second - 1
          if (that.second === 0) {
            that.is_send = 0
            that.second = 60
          } else {
            that.jian()
          }
        }, '1000')
      }
    },
    mounted () {
      if (this.currentUser.userId) {
        this.$Modal.info({
          title: '消息',
          content: '您已有账户，不需要注册！',
          onOk: () => {
            this.$router.push({ path: '/' })
          }
        })
      }
    },
    computed: {
      ...mapGetters(['currentToken', 'currentUser'])
    }
  }
</script>
<style scoped>
  .layout {
    border: 1px solid #d7dde4;
    background: #ccc;
  }

  .layout-logo {
    width: 200px;
    height: 40px;
    background: url('../../../../static/img/logo_bg.png') no-repeat;
    border-radius: 3px;
    float: left;
    position: relative;
    top: 15px;
    left: 20px;
  }

  .layout-nav {
    width: 600px;
    margin: 0 auto;
    color: #fff !important;
  }

  .layout-assistant {
    width: 300px;
    margin: 0 auto;
    height: inherit;
  }

  .layout-breadcrumb {
    padding: 10px 15px 0;
  }

  .layout-content {
    min-height: 200px;
    margin: 15px;
    overflow: hidden;
    background: #fff;
    border-radius: 4px;
  }

  .layout-content-main {
    padding: 10px;
  }

  .layout-copy {
    text-align: center;
    padding: 10px 0 20px;
    color: #9ea7b4;
  }

  .transportInfo {
    background: #fff;
    background-image: url('../../../../static/img/img_homebg01.png');
  }

  .transportQuery {
    background: #fff;
    background-image: url('../../../../static/img/img_homebg02.png');
  }

  .info {
    width: 960px;
    margin: 0 auto;
    height: 800px;
    /*background: yellow;*/
    overflow: hidden;
    position: relative;
  }

  .wrap {
    width: 100%;
    margin: 20px auto;
  }

  .station {
    height: 500px;
  }

  #footer {
    width: 100%;
    margin: 0 auto;
    height: 400px;
    background: #13293B;
    position: relative;
  }

  .footers {
    width: 900px;
    margin: 0 auto;
    margin-top: 60px;
    display: flex;
    justify-content: space-between;
  }

  .footers > div {
    /*float: left;*/

    font-size: 14px;
  }

  .footers > div > p {
    font-size: 18px;
    color: #FDFDFD;
    border-left: 1px solid #1B364D;
    padding-left: 15px;
  }

  .footers > .foo4 > p {
    font-size: 14px;
    color: #CACACA;
    margin-top: 3px;
  }

  .footers > .foo4 > img {
    display: inline-block;
    width: 96px;
    height: 88px;
    border-left: 1px solid #1B364D;
    padding-left: 15px;
    padding-top: 8px;
  }

  .footers > .foo4 > p:first-of-type {
    font-size: 18px;
    color: #FDFDFD;
  }

  .footers > div > p > a {
    text-decoration: none;
    font-size: 14px;
    color: #CACACA;
  }

  .mess {
    width: 70%;
    margin: 0 auto;
    margin-top: 60px;
    text-align: center;
  }

  #mianDeng {
    width: 960px;
    height: 160px;
    position: absolute;
    left: 50%;
    top: -50px;
    margin-left: -480px;
    background: #feca2e;
    border-bottom-left-radius: 25px;
    border-top-right-radius: 25px;
    z-index: 100;
    text-align: center;
  }

  #mianDeng > p:first-of-type {
    margin-top: 30px;
    font-size: 22px;
    color: #1B1E21;
  }

  #mianDeng > p:last-of-type {
    width: 120px;
    height: 40px;
    margin: 0 auto;
    margin-top: 30px;
    background-color: #1B1E21;
    line-height: 40px;
  }

  .demo-upload-list {
    display: inline-block;
    width: 60px;
    height: 60px;
    text-align: center;
    line-height: 60px;
    border: 1px solid transparent;
    border-radius: 4px;
    overflow: hidden;
    background: #fff;
    position: relative;
    box-shadow: 0 1px 1px rgba(0, 0, 0, .2);
    margin-right: 4px;
  }

  .demo-upload-list img {
    width: 100%;
    height: 100%;
  }

  .demo-upload-list-cover {
    display: none;
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background: rgba(0, 0, 0, .6);
  }

  .demo-upload-list:hover .demo-upload-list-cover {
    display: block;
  }

  .demo-upload-list-cover i {
    color: #fff;
    font-size: 20px;
    cursor: pointer;
    margin: 0 2px;
  }

  .ivu-icon {
    line-height: 58px;
  }
</style>
